<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
import { getPatientListApi } from '@/apis/patient';
import type { PatientItem } from '@/types/patient';
import { onShow } from '@dcloudio/uni-app';
import { ref } from 'vue'

const swipeOptions = ref([
  {
    text: '删除',
    style: {
      backgroundColor: '#dd524d',
      color: '',
      fontSize: '',
    },
  },
])

//1.获取患者列表
const patientList = ref<PatientItem[]>([])
const getPatientList = async () => {
  const res = await getPatientListApi()
  patientList.value = res
  
}
onShow(() => {
  getPatientList()
}) 

//2.删除患者
const onSwiperActionClick = async (id: string, index: number) => {
  //2.1调用删除接口
  await getPatientListApi()
  patientList.value.splice(index,1)
}
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item v-for="(item,index) in patientList" :key="item.id" :right-options="swipeOptions" @click="onSwiperActionClick(item.id!,index)">
          <view class="archive-card " :class="{active:item.defaultFlag===1}">
            <view class="archive-info">
              <text class="name">{{item.name}}</text>
              <!-- <text class="id-card">{{ item.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2') }}</text> -->
              <text class="default" v-if="item.defaultFlag===1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{item.genderValue}}</text>
              <text class="age">{{item.age}}岁</text>
            </view>
            <navigator hover-class="none" class="edit-link" :url="`/subpkg_archive/form/index?id=${item.id}`">
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList.length<6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/index">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
  </scroll-page>
</template>

<style lang="scss">
@import './index.scss';
</style>